package com.fuchuang.seckillsystem.controller;

import com.fuchuang.seckillsystem.entity.MyUserDetails;
import com.fuchuang.seckillsystem.service.ISeckillService;
import com.fuchuang.seckillsystem.vo.RespBean;
import com.fuchuang.seckillsystem.vo.SeckillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("seckill")
public class SeckillController {
    @Autowired
    private ISeckillService seckillService;
    
    @PostMapping("path")
    public RespBean getPath(@RequestBody SeckillVo seckillVo, @AuthenticationPrincipal MyUserDetails userDetails) {
        String path = seckillService.getSeckillPath(seckillVo.getActivityId(), seckillVo.getGoodsId(), userDetails.getId(), userDetails.getStatus());
        return RespBean.success(path);
    }
    
    @PostMapping("buy")
    public RespBean seckill(@RequestBody SeckillVo seckillVo, @AuthenticationPrincipal MyUserDetails userDetails) {
        seckillService.seckill(seckillVo.getActivityId(), seckillVo.getGoodsId(), seckillVo.getPath(), userDetails.getId());
        return RespBean.success();
    }
    
    @PostMapping("result")
    public RespBean getResult(@RequestBody SeckillVo seckillVo, @AuthenticationPrincipal MyUserDetails userDetails) {
        Integer result = seckillService.getResult(seckillVo.getActivityId(), seckillVo.getGoodsId(), userDetails.getId());
        return RespBean.success(result);
    }
    
    @PostMapping("stock")
    public RespBean getStock(@RequestBody SeckillVo seckillVo) {
        Integer stock = seckillService.getStock(seckillVo);
        return RespBean.success(stock);
    }
    
    
}
